using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// this encapsulate a Random object
/// and some methods to generate formated random code using the random object
/// </summary>
public class RandomHelper
{
    #region Members

    private int m_MaxLength = 0;
    private Random m_Random = null;
    private string m_Letters = "ABCDEFGHIJKLMNPQRSTUVWXYZ0123456789";

    #endregion

    #region Contructors

    public RandomHelper()
    {
        m_MaxLength = 5;
        m_Letters = "ABCDEFGHIJKLMNPQRSTUVWXYZ0123456789";
        m_Random = new Random();
    }

    public RandomHelper(int pMaxLength)
    {
        m_MaxLength = pMaxLength;
        m_Letters = "ABCDEFGHIJKLMNPQRSTUVWXYZ0123456789";
        m_Random = new Random();
    }

    public RandomHelper(string pLetters)
    {
        m_MaxLength = 5;
        m_Letters = pLetters;
        m_Random = new Random();
    }

    public RandomHelper(int pMaxLength, string pLetters)
    {
        m_MaxLength = pMaxLength;
        m_Letters = pLetters;
        m_Random = new Random();
    }

    #endregion

    #region Properties

    public int MaxLength
    {
        get
        {
            return m_MaxLength;
        }
        set
        {
            m_MaxLength = value;
        }
    }

    public string Letters
    {
        get
        {
            return m_Letters;
        }
        set
        {
            m_Letters = value;
        }
    }

    #endregion

    #region Method

    /// <summary>
    /// Returns a random string.
    /// </summary>
    /// <returns></returns>
    public string GenerateRandomCode()
    {
        int iLength = MaxLength;
        string sLetter = Letters;
        string sChar = "";

        string sResult = "";

        // make string
        for (int i = 0; i < iLength; i++)
        {
            sChar = sLetter.Substring(m_Random.Next(0, sLetter.Length - 1), 1);
            sResult = sResult + sChar;
        }

        return sResult;
    }

    #endregion Method
}
